﻿using Entity; 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ServiceCore.Tool.Text
{
    public interface IRW_Helper
    {
        /// <summary>
        /// 获取所有等级信息
        /// </summary>
        /// <returns></returns>
        List<repair_type> GetStructure_Template();
        /// <summary>
        /// 指定某项获取等级信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        List<repair_type> GetStructure_Template(Expression<Func<repair_type, bool>> where);
        /// <summary>
        ///  写入等级信息
        /// </summary>
        /// <param name="TempInfo"></param>
        void WriteTemplate(List<repair_type> TempInfo);
    }
    public class RW_Helper : IRW_Helper
    {
        private string basePath = AppContext.BaseDirectory + "/wwwroot/Config/";
        /// <summary>
        /// 定义缓存中的模板列表
        /// </summary>
        public static List<repair_type> TempInfo = new List<repair_type>();
        public RW_Helper() {

        }
        /// <summary>
        /// 进行一次文件初始化
        /// </summary>
        /// <returns></returns>
        public void Init()
        {
            if (!TempInfo.Any())
            {
                string TempList = ReadTemplate();
                try
                {
                    TempInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<repair_type>>(TempList);
                }
                catch { }
                //return this;
            }
        }
        /// <summary>
        /// 进行一次文件初始化
        /// </summary>
        /// <returns></returns>
        public  void Ref()
        {

            string TempList = ReadTemplate();
            try
            {
                TempInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<repair_type>>(TempList);
            }
            catch { }
        }
        /// <summary>
        /// 获取所有模板信息
        /// </summary>
        /// <returns></returns>
        public List<repair_type> GetStructure_Template()
        {
            Init();
            return TempInfo;
        }

        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <returns></returns>
        public List<repair_type> GetStructure_Template(Expression<Func<repair_type, bool>> where)
        {
            Init();
            return TempInfo.AsQueryable().Where(where).ToList();
        }
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="TempInfo"></param>
        public void WriteTemplate(List<repair_type> TempInfo)
        {
            try
            {
                //判断权限目录是否存在，如果不存在则开始创建       
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                string File_Path = basePath + "/repair_type_Config.json";
                //创建指定内容文件
                using (FileStream fs = new FileStream(File_Path, FileMode.Create))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(TempInfo));
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { Ref(); }
        } 
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="TempInfo"></param>
        private string ReadTemplate()
        {
            try
            {
                string File_Path = basePath + "/repair_type_Config.json";
                if (!File.Exists(File_Path))
                {
                    TemplateFileExist();
                }
                using (FileStream fS = new FileStream(File_Path, FileMode.Open, FileAccess.Read))
                {
                    StreamReader sr = new StreamReader(fS, Encoding.UTF8);//设定读写的编码                 
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 如果文件不存在，则创建一个只有普通用户的默认等级
        /// </summary>
        private void TemplateFileExist()
        {
            string File_Path = basePath + "/repair_type_Config.json";
            if (!File.Exists(File_Path))
            {
                List<repair_type> NewTemp = new List<repair_type>();
                repair_type NewItem = new repair_type();
                NewTemp.Add(new repair_type() { type_name = "A1付款不出货问题", kid_type_name = new string[] { "1.IC主板程序问题", "2.IC2驱动板程序问题", "3.单片机手机卡欠费", "4.信号问题" } });
                NewTemp.Add(new repair_type() { type_name = "A2公众号问题", kid_type_name = new string[] { "1.公众号没有提醒", "2.公众号无法绑定" } });
                NewTemp.Add(new repair_type() { type_name = "A3后台问题", kid_type_name = new string[] { "1.库存问题", "2.结算问题", "3.交易记录问题", "3.交易记录问题", "4.流量卡绑定", "5.账户激活与绑定", "6.图片与价格设置", "7.退款问题", "8.温度与灯光设置", "9.京东结算资料问题" } });
                NewTemp.Add(new repair_type() { type_name = "A4程序更新升级", kid_type_name = new string[] { "1.刷脸程序安装", "2.主程序&守护程序安装" } });

                NewTemp.Add(new repair_type() { type_name = "B1控制部件", kid_type_name = new string[] { "1.M4G控制板", "2.1ST控制板", "3.2ND控制板", "4.3RD控制板", "5.AMD驱动板", "6.安卓主板", "7.TTL转RS485通讯板", "8.温控主板", "9.小转接板", "10.升压板", "11.绿色主板", "12.绿色驱动板", "13.键盘", "14.竖梁转接板" } });
                NewTemp.Add(new repair_type() { type_name = "B2可视化", kid_type_name = new string[] { "1.22寸触摸屏", "2.22寸显示屏", "3.10寸触摸显示屏", "4.32寸显示屏", "5.55寸显示屏", "6.人脸识别摄像头", "7.单片机显示屏" } });
                NewTemp.Add(new repair_type() { type_name = "B3制冷/热系统", kid_type_name = new string[] { "1.压缩机罐", "2.贯流风机", "3.散热风扇", "4.加热块" } });
                NewTemp.Add(new repair_type() { type_name = "B4供电系统", kid_type_name = new string[] { "1.开关电源24V150W", "2.适配器12V5A", "3.品字插座", "4.船型开关", "5.断路器（空气开关/空开）", "6.电线" } });
                NewTemp.Add(new repair_type() { type_name = "B5负载", kid_type_name = new string[] { "1.电机", "2.电机线", "3.掉货检测", "4.电磁锁", "5.喇叭", "6.灯带", "7.信号天线", "8.通讯线", "9.副柜连接线", "10.玻璃加热" } });
                NewTemp.Add(new repair_type() { type_name = "B6物理结构", kid_type_name = new string[] { "1.玻璃破损", "2.钥匙锁", "3.格子柜门", "4.取物口无法开启", "5.电机弹簧装配", "6.滑道调整", "7.取物口无法开启" } });

                NewTemp.Add(new repair_type() { type_name = "C1机器无网络", kid_type_name = new string[] { "1.无法生成二维码", "2.IC提示离线", "3.IC2无法进入系统" } });
                NewTemp.Add(new repair_type() { type_name = "C2机器日常咨询", kid_type_name = new string[] { "1.咨询购买配件", "2.转让机器指导使用", "3.新机指导调试", "4.机器规格咨询", "5.手机卡流量咨询", "6.转让咨询", "7.后台无法登陆", "8.银行卡不到账" } });
                NewTemp.Add(new repair_type() { type_name = "C3投币/纸币/退币器", kid_type_name = new string[] { "1.投币器卡币排除", "2.退币器不找零", "3.退币器找零不准", "4.退币器一直退币", "5.纸币器不收钱", "6.纸币器不找零", "7.不识别新版纸币" } });
                NewTemp.Add(new repair_type() { type_name = "C4变更收款账户", kid_type_name = new string[] { "1.机器转让过户", "2.变更银行卡" } });
                NewTemp.Add(new repair_type() { type_name = "C5操作问题", kid_type_name = new string[] { "1.按键提示编号不存在", "2.按键提示货道故障", "3.提示“停止售卖”", "4.单片机不扫码出货", "5.单片机无法进入系统", "6.温度设置问题", "7.指导补货", "8.纸币器存取款" } });
                NewTemp.Add(new repair_type() { type_name = "C6改系统", kid_type_name = new string[] { "1.按键改4G系统", "2.金吉机器改系统", "3.按键改刷脸系统", "4.触摸改刷脸系统" } });
                NewTemp.Add(new repair_type() { type_name = "C7后台升级咨询", kid_type_name = new string[] { "1.1号转3号后台升级", "2.汇聚转京东后台升级" } });


                NewTemp.Add(new repair_type() { type_name = "D1按键", kid_type_name = new string[] { "1.硬币退币器", "2.金属键盘" } });
                NewTemp.Add(new repair_type() { type_name = "D2电路板", kid_type_name = new string[] { "1.绿色按键驱动板", "2.黑色驱动板", "3.4G主板", "4.按键主板", "5.安卓板+4G模块", "6.升压板"} });
                NewTemp.Add(new repair_type() { type_name = "D3改系统", kid_type_name = new string[] { "1.10寸刷脸外挂屏", "2.按键改无屏扫码(不带副柜)", "3.按键改无屏扫码(一拖一)", "4.按键改无屏扫码(一拖二)", "5.按键改无屏扫码(一拖三)", "6.金吉改系统(一拖一)", "7.金吉改系统(一拖二)", "8.金吉改系统(一拖三)" } });
                NewTemp.Add(new repair_type() { type_name = "D4货道组件", kid_type_name = new string[] { "1.货道单电机", "2.货道双电机", "3.隔板", "4.货道弹簧", "5.主机价格标签", "6.副机价格标签", "7.整层托盘带电机弹簧和电机线" } });
                NewTemp.Add(new repair_type() { type_name = "D5电源", kid_type_name = new string[] { "1.正品明纬电源" } });
                NewTemp.Add(new repair_type() { type_name = "D6开关", kid_type_name = new string[] { "1.漏电开关" } });
                NewTemp.Add(new repair_type() { type_name = "D7屏幕", kid_type_name = new string[] { "1.触摸屏幕", "2.按键显示屏", "3.16寸显示屏", "4.22寸显示屏" } });
                NewTemp.Add(new repair_type() { type_name = "D8软件", kid_type_name = new string[] { "1.改后台", "2.纸币器升级" } });
                NewTemp.Add(new repair_type() { type_name = "D9锁", kid_type_name = new string[] { "1.副柜电子锁", "2.主机门锁", "3.小机器锁" } });
                NewTemp.Add(new repair_type() { type_name = "D10贴纸", kid_type_name = new string[] { "1.柜门贴纸", "2.取物口贴纸"} });
                NewTemp.Add(new repair_type() { type_name = "D11物理配件", kid_type_name = new string[] { "1.取物口", "2.副柜耐力板", "3.副柜门框", "4.饮料机主柜小门", "5.玻璃柜门", "6.取物口外侧挡板", "7.取物口内侧挡板", "8.滑道", "9.适配器", "10.温控器", "11.新副柜塑料门", "12.扬声器", "13.刷脸摄像头", "14.灯条" } });
                NewTemp.Add(new repair_type() { type_name = "D12线", kid_type_name = new string[] { "1.信号天线(4G)", "2.信号天线(2G)", "3.副柜链接线1.5米", "4.副柜链接线3米", "5.副柜链接线5米", "6.电源线", "7.电机线", "8.退币器连接线", "9.灯条连接线" } });
                NewTemp.Add(new repair_type() { type_name = "D13现金设备", kid_type_name = new string[] { "1.纸币器", "2.找零头ITL", "3.硬币投币器" } });
                NewTemp.Add(new repair_type() { type_name = "D14制冷系统", kid_type_name = new string[] { "1.压缩机", "2.加热模块", "3.IC，IC2加热板(收500元，押金退回返200元)", "4.温控板", "5.贯流风机", "6.散热风扇电机" } });
                NewTemp.Add(new repair_type() { type_name = "D15流量卡", kid_type_name = new string[] { "1.流量卡" } }); 

                NewTemp.Add(new repair_type() { type_name = "Z其他", kid_type_name = new string[] { "1其他"  } });
                //直接按照自定义模板创造一个文件
                WriteTemplate(NewTemp);
            }
        }
    }
}
